/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.etop.ondoc.beans;

import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.CascadeType;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * 设置了表示用户权限、记录是否有效的常量
 * @author jansing lz_jansing@163.com
 */
@Entity
@Table(name = "t_user")
public class User {
    public static final int ROLE_USER = 1;
    public static final int ROLE_ADMIN = 2;
    public static final int IS_VAILD = 1;
    public static final int IS_NOT_VAILD = 0;
    
    
    private int id;
    /**
     * 用户名为必填，用于显示在页面上
     */
    private String username;
    private String password;
    /**
     * 手机号码
     */
    private String tele;
    private String email;
    /**
     * 头像保存的路径
     */
    private String headpic;
    /**
     * 该用户上传的文档总数
     */
    private int uptotal;
    /**
     * 该用户下载的文档总数
     */
    private int downtotal;
    /**
     * 该用户收藏的文档总数
     */
    private int collecttotal;
    /**
     * 1为普通用户，2为管理员<br/>
     * 因为role是数据库的保留字，所以数据库的字段改成user_role
     */
    private int role;
    /**
     * 1：有效，0：无效
     */
    private int isVaild;
    private Date createDate;
    private Set<Document> upSet;
    /**
     * 用户举报建议以及管理员的举报反馈建议反馈
     */
    private Set<Feedback> feedSet;

    /**
     * isVaild、role、upSet、feedSet、headpic属性已初始化，<br/>
     * tele、email可以为空<br/>
     * 其他属性都要<br/>
     */
    public User() {
        this.isVaild = IS_VAILD;
        this.role = ROLE_USER;
        upSet = new HashSet<>();
        feedSet = new HashSet<>();
    }

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    /**
     * 用户名为必填，用于显示在页面上
     */
    @Column(name = "username")
    public String getUsername() {
        return username;
    }

    /**
     * 用户名为必填，用于显示在页面上
     */
    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "password")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * 手机号码
     */
    @Column(name = "tele")
    public String getTele() {
        return tele;
    }

    /**
     * 手机号码
     */
    public void setTele(String tele) {
        this.tele = tele;
    }

    @Column(name = "email")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * 头像保存的路径
     */
    @Column(name = "head_pic")
    public String getHeadpic() {
        return headpic;
    }

    /**
     * 头像保存的路径
     */
    public void setHeadpic(String headpic) {
        this.headpic = headpic;
    }

    /**
     * 该用户上传的文档总数
     */
    @Column(name = "up_total")
    public int getUptotal() {
        return uptotal;
    }

    /**
     * 该用户上传的文档总数
     */
    public void setUptotal(int uptotal) {
        this.uptotal = uptotal;
    }

    /**
     * 该用户下载的文档总数
     */
    @Column(name = "down_total")
    public int getDowntotal() {
        return downtotal;
    }

    /**
     * 该用户下载的文档总数
     */
    public void setDowntotal(int downtotal) {
        this.downtotal = downtotal;
    }

    /**
     * 1为普通用户，2为管理员<br/>
     * 因为role是数据库的保留字，所以数据库的字段改成user_role
     */
    @Column(name = "user_role")
    public int getRole() {
        return role;
    }

    /**
     * 1为普通用户，2为管理员<br/>
     * 因为role是数据库的保留字，所以数据库的字段改成user_role
     */
    public void setRole(int role) {
        this.role = role;
    }

    /**
     * 1：有效，0：无效
     */
    @Column(name = "is_vaild")
    public int getIsVaild() {
        return isVaild;
    }

    /**
     * 1：有效，0：无效
     */
    public void setIsVaild(int isVaild) {
        this.isVaild = isVaild;
    }

    @Column(name = "create_date")
    @Temporal(value = TemporalType.TIMESTAMP)
    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    /**
     * 该用户收藏的文档总数
     */
    @Column(name = "collect_total")
    public int getCollecttotal() {
        return collecttotal;
    }

    /**
     * 该用户收藏的文档总数
     */
    public void setCollecttotal(int collecttotal) {
        this.collecttotal = collecttotal;
    }

    /**
     * 显示用户并不需要知道他上传的文档，所以懒加载
     *
     * @return
     */
    @OneToMany(mappedBy = "upUser", fetch = FetchType.LAZY, cascade = {CascadeType.REMOVE})
    public Set<Document> getUpSet() {
        return upSet;
    }

    public void setUpSet(Set<Document> upSet) {
        this.upSet = upSet;
    }

    /**
     * 用户举报建议以及管理员的举报反馈建议反馈 显示用户并不需要知道他举报、建议的消息，所以懒加载
     *
     * @return
     */
    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = {CascadeType.REMOVE})
    public Set<Feedback> getFeedSet() {
        return feedSet;
    }

    /**
     * 用户举报建议以及管理员的举报反馈建议反馈
     */
    public void setFeedSet(Set<Feedback> feedSet) {
        this.feedSet = feedSet;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + Objects.hashCode(this.username);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final User other = (User) obj;
        if (!Objects.equals(this.username, other.username)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        String roleString = null;
        if (role == 2) {
            roleString = "管理员";
        } else if (role == 1) {
            roleString = "普通用户";
        }
        
        String s = null;
        try{
            s = "User{" + "id=" + id + ", username=" + username + ", password=" + password + ", tele=" + tele + ", email=" + email + ", uptotal=" + uptotal + ", downtotal=" + downtotal + ", collecttotal=" + collecttotal + ", role=" + roleString + ", isVaild=" + isVaild + ", createDate=" + createDate + '}';
        } catch(Exception e){
            System.err.println("User实体类的toString错误，请查看源码");
        }
        return s;
    }

}
